package com.health.db.dao.user.impl;
 
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.health.db.dao.ServiceInterface;
import com.health.db.dao.user.IUserCollectFavoriteDao;
import com.health.db.dto.DoctorListDto;
import com.health.db.mapper.UserCollectFavoriteMapper;
import com.health.db.model.UserCollectFavorite;
import com.health.redis.RedisConstants;

/**
 * Created by helili on 2017/2/21.
 */
public class UserCollectFavoriteDaoImpl extends ServiceInterface implements IUserCollectFavoriteDao {

     
	UserCollectFavoriteMapper userCollectFavoriteMapper;
	
	private String selectByCollectorId=":collectorId:";
	private String selectByCollectionId=":collectionId:";

    @Override
    public int deleteByPrimaryKey(UserCollectFavorite record) {
       
        int  delete = userCollectFavoriteMapper.deleteByPrimaryKey(record.getCollectid());
        
        
        getRedisOperatiorLogic();
        if(redisOperatiorLogic.isJedisIsConnected())
		{
        	String key1=RedisConstants.UserCollectFavoriteMapper+selectByCollectorId+record.getCollectorid();
        	String key2=RedisConstants.UserCollectFavoriteMapper+selectByCollectorId+ record.getCollectorid()+selectByCollectionId+ record.getCollectionid();
			redisOperatiorLogic.deleteObject(key1, getOrderRedisGroupNo("1"));
			redisOperatiorLogic.deleteObject(key2, getOrderRedisGroupNo("1"));
		}
        
        return delete;
    }

    @Override
    public int insert(UserCollectFavorite record) {
 
        int insert = userCollectFavoriteMapper.insert(record);
        
    	getRedisOperatiorLogic();
        if(redisOperatiorLogic.isJedisIsConnected())
		{
        	String key1=RedisConstants.UserCollectFavoriteMapper+selectByCollectorId+record.getCollectorid();
        	String key2=RedisConstants.UserCollectFavoriteMapper+selectByCollectorId+ record.getCollectorid()+selectByCollectionId+ record.getCollectionid();
			redisOperatiorLogic.deleteObject(key1, getOrderRedisGroupNo("1"));
			redisOperatiorLogic.deleteObject(key2, getOrderRedisGroupNo("1"));
		}
        
        return insert;
    }


    @Override
    public List<DoctorListDto> selectDocListDtoByCollectorId(Long collectorId) {
        
    	getRedisOperatiorLogic();
     	String key=RedisConstants.UserCollectFavoriteMapper+selectByCollectorId+collectorId;
     	Object object=null;
      
     	List<DoctorListDto> userCollectFavoriteList = null;
     		
 		 if(redisOperatiorLogic.isJedisIsConnected())
 		 {
 			 object=redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
 			 
 		 }
 		 if(null!=object )
 		 {
 			 if (!"".equals(object)) {
 				userCollectFavoriteList=(List<DoctorListDto>) object;
			}
 			
 		 }
 		 else
 		 {
 			userCollectFavoriteList = userCollectFavoriteMapper.selectDocListDtoByCollectorId(collectorId);
 			
 			if(redisOperatiorLogic.isJedisIsConnected())
    		{
 				redisOperatiorLogic.setEx(key, userCollectFavoriteList, getOrderRedisGroupNo("1"));
    		}
 		 }
        
        return userCollectFavoriteList;
    }

    @Override
    public UserCollectFavorite selectByCollectorIdAndCollectionId(Long collectorId, Long collectionId) {
    	
    	getRedisOperatiorLogic();
    	String key=RedisConstants.UserCollectFavoriteMapper+selectByCollectorId+collectorId+selectByCollectionId+collectionId;
    	Object object=null;
    	UserCollectFavorite collectFavorite=null;
    
		 if(redisOperatiorLogic.isJedisIsConnected())
		 {
			 object=redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
		 }
		 if(null!=object)
		 {
			if (!"".equals(object)) {
			 collectFavorite=(UserCollectFavorite) object;
			}
		 }
		 else
		 {
			 collectFavorite = userCollectFavoriteMapper.selectByCollectorIdAndCollectionId(collectorId,collectionId);

			 if(redisOperatiorLogic.isJedisIsConnected())
    		 {
					 redisOperatiorLogic.setEx(key, collectFavorite, getOrderRedisGroupNo("1"));
    		 }
		 }
        return collectFavorite;
    }
    

	public UserCollectFavoriteMapper getUserCollectFavoriteMapper() {
		return userCollectFavoriteMapper;
	}

	public void setUserCollectFavoriteMapper(
			UserCollectFavoriteMapper userCollectFavoriteMapper) {
		this.userCollectFavoriteMapper = userCollectFavoriteMapper;
	}

	 


}
